﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters.Login
{
    public class UserLogin
    {

        public UserLogin()
        {

            dataContext = new DSSEntities();

        }

        DSSEntities dataContext;

        public string base64Encode(string sData)
        {
            try
            {
                byte[] encDataByte = new byte[sData.Length];
                encDataByte = System.Text.Encoding.UTF8.GetBytes(sData);
                string encodedData = Convert.ToBase64String(encDataByte);
                return encodedData;
            }
            catch (Exception ex)
            {
                throw new Exception("Error in base64Encode" + ex.Message);
            }
        }

        public string base64Decode(string sData)
        {

            try
            {
                System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
                System.Text.Decoder utf8Decode = encoder.GetDecoder();
                byte[] todecode_byte = Convert.FromBase64String(sData);
                int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                return result;
            }
            catch (Exception ex)
            {

                throw new Exception("Error in base64Dncode" + ex.Message);
            }

        }

        

        public Boolean UserLoginClick(String username, String password , Boolean isCheckForSysAdmin)
        {
            string encriptPassword = String.Empty;

            encriptPassword = base64Encode(password);
            IQueryable<MemberLogin> query;
            if (isCheckForSysAdmin)
            {
                query = (
                  from c in dataContext.MemberLogin
                  where c.login_username == username && c.login_password == encriptPassword && c.login_isCEO == true && c.login_isApproved == true
                  select c);
            }
            else
            {

                query = (
                    from c in dataContext.MemberLogin
                    where c.login_username == username && c.login_password == encriptPassword && c.login_isApproved == true
                    select c);
            }
            List<MemberLogin> selectedMember = query.ToList();
            Boolean isValidate = false;
            if (selectedMember.Count == 1) isValidate = true;
            return isValidate;

        }


        public void isAdmin(String username, String password , out Boolean isValide , out Members mem )
        {
            string encriptPassword = "";

            encriptPassword = base64Encode(password);
            IQueryable<MemberLogin> query;
            IQueryable<MemberLogin> queryMemName;
            String memRef = "" ;
            query = (
                 from c in dataContext.MemberLogin
                 where c.login_username == username && c.login_password == encriptPassword && c.login_isCEO == true && c.login_isApproved == true
                 select c);

            if (query.ToList().Count == 0)
            {
                queryMemName = (
                     from c in dataContext.MemberLogin
                     where c.login_username == username && c.login_password == encriptPassword && c.login_isApproved == true
                     select c);
                List<MemberLogin> selectedMember1 = queryMemName.ToList();
                memRef = selectedMember1.First().mem_reference.ToString();
                mem = dataContext.Members.First(i => i.mem_reference.Equals(memRef));
                isValide = false;
            }
            else {
                List<MemberLogin> selectedMember = query.ToList();
                memRef = selectedMember.First().mem_reference.ToString();
                isValide = true;
                mem = dataContext.Members.First(i => i.mem_reference.Equals(memRef));
            }
              
           

            
            
        }

        public String UserExitClick(String password)
        {

            return base64Decode(password);

        }
    }
}
